const { Op } = require("sequelize");
const RoleModels = require('../models/role');
const RoleToPermisModels = require('../models/role_permis')
const PermisModels = require('../models/permis')

interface Role {
    identity: number,
    label: string,
    role_name: string,
    state?: number
    describes?: string
}

class RoleService {
    // 获取角色（权限）列表
    public async getRoleList() {
        const res: any = await RoleModels.findAndCountAll()
        return res ? res : null
    }
    // 分页获取角色（权限）列表
    public async getRoleListByPageInfo(pageNum: number, pageSize: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await RoleModels.findAndCountAll({ offset: offset, limit: pageSize * 1 })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据名字获取角色
    public async searchRolesByname({ name, pageNum, pageSize }) {
        if (pageNum && pageSize) {
            // 分页获取
            const offset = (pageNum - 1) * pageSize
            const { count, rows } = await RoleModels.findAndCountAll({
                where: {
                    role_name: {
                        [Op.like]: `%${name}%`,
                    }
                },
                offset: offset,
                limit: pageSize * 1
            })
            return {
                pageNum,
                pageSize,
                total: count,
                list: rows
            }
        } else {
            // 不分页直接获取
            const { count, rows } = await RoleModels.findAndCountAll({
                where: {
                    role_name: {
                        [Op.like]: `%${name}%`,
                    }
                }
            })
            return {
                total: count,
                list: rows
            }
        }
    }
    // 获取角色对应的权限
    public async getPermissionByRoles() {
        const { count, rows } = await RoleToPermisModels.findAndCountAll({
            include: {
                model: PermisModels,
                as: 'permis_info'
            }
        })
        return {
            total: count,
            list: rows
        }
    }
    // 根据id获取角色对应的权限
    public async getPermissionByRoleId(id: number) {
        const { count, rows } = await RoleToPermisModels.findAndCountAll({
            where: {
                role_id: id
            },
            include: {
                model: PermisModels,
                as: 'permis_info'
            }
        })
        return {
            total: count,
            list: rows
        }
    }
    // 根据id获取角色
    public async getRoleByIdentity(identity: number) {
        const res = await RoleModels.findOne({
            where: { identity }
        })
        return res
    }
    // 添加角色
    public async creatRole(params: Role) {
        const res: any = await RoleModels.create(params)
        return res.dataValues;
    }
    // 更新角色
    public async updateRoleInfo(identity: number, params: Role) {
        const whereOpt = { identity }
        const res = await RoleModels.update(params, { where: whereOpt })
        return res[0] > 0
    }
    // 删除角色
    public async deleteById(identity: number) {
        const res: any = await RoleModels.destroy({
            where: { identity }
        })
        return res > 0
    }
}

module.exports = new RoleService()
// 解决同目录下多个ts文件都引入Op,会报错的问题
export {}